package com.funo.wcity.app.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.funo.wcity.app.entity.LoginUser;
import com.funo.wcity.app.entity.TblCompanies;
import com.funo.wcity.app.entity.TblContactPersons;
import com.funo.wcity.app.entity.TblMailDetail;
import com.funo.wcity.app.service.LoginUserService;
import com.funo.wcity.app.service.TblCompaniesService;
import com.funo.wcity.app.service.TblCompanyTypesService;
import com.funo.wcity.app.service.TblContactPersonsService;
import com.funo.wcity.app.service.TblMailDetailService;
import com.funo.wcity.app.service.TblemployeesService;
import com.funo.wcity.app.utils.Constant;
import com.funo.wcity.app.utils.JsonUtil;
import com.funo.wcity.app.utils.MD5;

@Controller
@SuppressWarnings("all")
public class ContactPersonController extends CommonController{
	@Resource(name="loginUserService")
	private LoginUserService loginUserService;
	@Resource(name="tblemployeesService")
	private TblemployeesService tblemployeesService;
	@Resource(name="tblContactPersonsService")
	private TblContactPersonsService tblContactPersonsService;
	@Resource(name="tblCompaniesService")
	private TblCompaniesService tblCompaniesService;
	@Resource(name="tblCompanyTypesService")
	private TblCompanyTypesService tblCompanyTypesService;
	@Resource(name="tblMailDetailService")
	private TblMailDetailService tblMailDetailService;
	/**
	 * 查询联系人列表
	 */
	@RequestMapping("/contactList")
	public String contactList(HttpServletRequest request,HttpServletResponse response,Integer idCompany){
		String idEmployees=(String)request.getSession().getAttribute(Constant.LOGIN_EMPLOYEE);
		String companyName = "";
		try{
			//查询所有公司
			List tblCompaniesList=this.tblCompaniesService.getTblCompaniesList(idEmployees);
			//查询所有公司类型
			List tblCompanyTypesList=this.tblCompanyTypesService.getTblCompanyTypesList(idEmployees);
			//查询所有联系人
			List<TblContactPersons> tblContactPersonsList=this.tblContactPersonsService.getContactPersonslListByHibernate(idEmployees,idCompany);
			if(idCompany!=null){
				TblCompanies company = this.tblCompaniesService.getTblCompaniesById(idCompany);
				request.setAttribute("idCompany", idCompany);
				request.setAttribute("companyName", company.getCompanyName());
				request.setAttribute("idCompanyType", company.getIdCompanyType());
			}
			request.setAttribute("tblCompaniesList", tblCompaniesList);
			request.setAttribute("tblCompanyTypesList", tblCompanyTypesList);
			request.setAttribute("tblContactPersonsList",tblContactPersonsList);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "web/addr_listall";
	}
	/**
	 * 跳转新建联系人
	 */
	@RequestMapping("/intoCreateContact")
	public String intoCreateContact(HttpServletRequest request,HttpServletResponse response){
		String idEmployees=(String)request.getSession().getAttribute(Constant.LOGIN_EMPLOYEE);
		try{
			//查询所有公司
			List tblCompaniesList=this.tblCompaniesService.getTblCompaniesList(idEmployees);
			//查询所有公司类型
			List tblCompanyTypesList=this.tblCompanyTypesService.getTblCompanyTypesList(idEmployees);
			request.setAttribute("tblCompaniesList", tblCompaniesList);
			request.setAttribute("tblCompanyTypesList", tblCompanyTypesList);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "web/new_contact";
	}
	/**
	 * 新建联系人
	 */
	@RequestMapping("/createContact")
	public String createContact(HttpServletRequest request,HttpServletResponse response,TblContactPersons contactPerson){
		try{
			contactPerson.setLastDate(new Date());
			contactPerson.setIsDefault(false);
			this.tblContactPersonsService.createTblContactPersons(contactPerson);
		}catch(Exception e){
			e.printStackTrace();
		}
		return this.contactList(request,response,contactPerson.getTblcompanies().getIdCompany());
	}
	
	/**
	 * 删除联系人
	 */
	@RequestMapping("/deleteContact")
	public String deleteContact(HttpServletRequest request,HttpServletResponse response,String idContactPersons){
		try{
			this.tblContactPersonsService.deleteTblContactPersons(idContactPersons);
		}catch(Exception e){
			e.printStackTrace();
		}
		return this.contactList(request,response,null);
	}
	
	/**
	 * 跳转修改联系人
	 */
	@RequestMapping("/intoUpdateContact")
	public String intoUpdateContact(HttpServletRequest request,HttpServletResponse response,Integer idContactPerson){
		String idEmployees=(String)request.getSession().getAttribute(Constant.LOGIN_EMPLOYEE);
		try{
			//查询所有公司
			List tblCompaniesList=this.tblCompaniesService.getTblCompaniesList(idEmployees);
			//查询所有公司类型
			List tblCompanyTypesList=this.tblCompanyTypesService.getTblCompanyTypesList(idEmployees);
			TblContactPersons contactPerson = this.tblContactPersonsService.getTblContactPersons(idContactPerson);
			
			request.setAttribute("tblCompaniesList", tblCompaniesList);
			request.setAttribute("tblCompanyTypesList", tblCompanyTypesList);
			request.setAttribute("contactPerson", contactPerson);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "web/update_contact";
	}
	
	/**
	 * 修改联系人
	 */
	@RequestMapping("/updateContact")
	public String updateContact(HttpServletRequest request,HttpServletResponse response,TblContactPersons contactPerson){
		try{
			contactPerson.setLastDate(new Date());
			this.tblContactPersonsService.updateTblContactPersons(contactPerson);
		}catch(Exception e){
			e.printStackTrace();
		}
		return this.contactList(request,response,contactPerson.getTblcompanies().getIdCompany());
	}
	/**
	 * 跳转新建公司
	 */
	@RequestMapping("/intoCreateCompany")
	public String intoCreateCompany(HttpServletRequest request,HttpServletResponse response,Integer idCompanyType){
		String idEmployees=(String)request.getSession().getAttribute(Constant.LOGIN_EMPLOYEE);
		try{
			//查询所有公司类型
			List tblCompanyTypesList=this.tblCompanyTypesService.getTblCompanyTypesList(idEmployees);
			request.setAttribute("tblCompanyTypesList", tblCompanyTypesList);
			request.setAttribute("idCompanyType", idCompanyType);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "web/new_company";
	}
	/**
	 * 新建公司
	 */
	@RequestMapping("/createCompany")
	public String createCompany(HttpServletRequest request,HttpServletResponse response,TblCompanies company){
		try{
			company.setLastDate(new Date());
			company.setCreateDate(new Date());
			LoginUser loginUser = (LoginUser)request.getSession().getAttribute(Constant.LOGIN_USER);
			company.setIdUser(loginUser.getIdUser());
			company.setIdEmployee(loginUser.getIdEmployee());
			this.tblCompaniesService.createTblCompanies(company);
		}catch(Exception e){
			e.printStackTrace();
		}
		return this.contactList(request,response,company.getIdCompany());
	}
	/**
	 * 删除公司
	 */
	@RequestMapping("/deleteCompany")
	public String deleteCompany(HttpServletRequest request,HttpServletResponse response,Integer idCompany){
		try{
			TblCompanies company = this.tblCompaniesService.getTblCompaniesById(idCompany);
			this.tblCompaniesService.deleteTblCompaniesById(company);
		}catch(Exception e){
			e.printStackTrace();
		}
		return this.contactList(request,response,null);
	}
	/**
	 * 跳转修改公司
	 */
	@RequestMapping("/intoUpdateCompany")
	public String intoUpdateCompany(HttpServletRequest request,HttpServletResponse response,Integer idCompany){
		String idEmployees=(String)request.getSession().getAttribute(Constant.LOGIN_EMPLOYEE);
		try{
			//查询所有公司类型
			List tblCompanyTypesList=this.tblCompanyTypesService.getTblCompanyTypesList(idEmployees);
			TblCompanies company = this.tblCompaniesService.getTblCompaniesById(idCompany);
			
			request.setAttribute("tblCompanyTypesList", tblCompanyTypesList);
			request.setAttribute("company", company);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "web/update_company";
	}
	
	/**
	 * 修改公司
	 */
	@RequestMapping("/updateCompany")
	public String updateCompany(HttpServletRequest request,HttpServletResponse response,TblCompanies company,String createDateTemp){
		try{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			company.setLastDate(new Date());
			company.setCreateDate(sdf.parse(createDateTemp));
			this.tblCompaniesService.updateTblCompanies(company);
		}catch(Exception e){
			e.printStackTrace();
		}
		return this.contactList(request,response,company.getIdCompany());
	}
	
	/**
	 * 查询联系人列表
	 */
	@RequestMapping("/queryContact")
	public String queryContact(HttpServletRequest request,HttpServletResponse response,String queryStr){
		String idEmployees=(String)request.getSession().getAttribute(Constant.LOGIN_EMPLOYEE);
		try{
			queryStr=java.net.URLDecoder.decode(queryStr,"utf-8");
			//查询所有公司
			List tblCompaniesList=this.tblCompaniesService.getTblCompaniesList(idEmployees);
			//查询所有公司类型
			List tblCompanyTypesList=this.tblCompanyTypesService.getTblCompanyTypesList(idEmployees);
			//查询所有联系人
			List<TblContactPersons> tblContactPersonsList=this.tblContactPersonsService.getContactPersonslListByLike(idEmployees,queryStr);
			request.setAttribute("tblCompaniesList", tblCompaniesList);
			request.setAttribute("tblCompanyTypesList", tblCompanyTypesList);
			request.setAttribute("tblContactPersonsList",tblContactPersonsList);
			request.setAttribute("queryStr", queryStr);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "web/addr_listall";
	}
}
